/*
 *	Copyright (C) 2008 CRIMERE
 *	Copyright (C) 2008 Jean-Marc Mercier
 *	
 *	This file is part of OTS (optimally transported schemes), an open-source library
 *	dedicated to scientific computing. http://code.google.com/p/optimally-transported-schemes/
 *
 *	CRIMERE makes no representations about the suitability of this
 *	software for any purpose. It is provided "as is" without express or
 *	implied warranty.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include <set>


#include <src/algorithm/random/NormalGenerator.h>
#include <src/math/Operators/all.h>
#include <src/algorithm/Evolution_Algorithm/Crank_Nicolson.h>
#include <src/algorithm/Evolution_Algorithm/ForwardEuler.h>
#include <src/math/BoundaryCondition/BoundaryCondition.h>
#include <src/math/CauchyProblems/CauchyProblem_traits.h>
#define NUMBER_OF_PARTICLES 10
template <class data>
struct CrankNicolson_D_x : public Crank_Nicolson<D_x_t<data>>
{
	OS_STATIC_CHECK(data::Dim == 1); // not sure of the multidimensional behavior. To test before.
	virtual OS_double CFL() {
		return .5/ get_state_variable()->size();
	};
	virtual bool criteria() {
		OS_double breakpoint1 = jmmath::norm22(Op_iterate - Op_S_k1);
		OS_double breakpoint2 = jmmath::norm22(Op_iterate);
		return breakpoint1/(1.+breakpoint2) < 1e-4;
	};
};

#if !defined(_Cauchy_Delta_x_)
#define _Cauchy_Delta_x_

//! Basic class to illustrate numerical behavior of a forward finite difference operator modelizing the Cauchy Problem \f$\partial_t u(t,x) = \partial_x u(t,x)\f$.
/*!
	This class initialize the Cauchy problem for the one dimensional transport equation
	\f[
		\partial_t u(t,x) = \partial_x u(t,x)
	\f];
	with \f$x \in [0,1]\f$, \f$t\ge 0\f$.

	It illustrate the instable long term behavior of the finite difference scheme
	\f[
		\delta_t u^n = \delta_{x} u^{n+1/2} = 0
	\f];
	The algorithm used is Crank Nicolson.
	The boundary conditions are given by <data> : either Dirichlet or Periodic ones.
	The initial data corresponds to a normal distribution centered in 0.5.
*/
template <class data, class Interface = Graphical_Interface>
		class Cauchy_D_x : public CauchyProblem_traits< CrankNicolson_D_x < data >, Interface>
		{
		public :
			Cauchy_D_x(){
				initial_conditions(NUMBER_OF_PARTICLES);
			};
			virtual smart_ptr<interface_type> Create_Instance() {
				return smart_ptr<interface_type>(new Cauchy_D_x);
			};

			virtual void set_dim(OS_size dim) {
				OS_DYNAMIC_CHECK(dim == 1,"try to initialise a one dimensional Cauchy problem Cauchy_D_x with an higher dimension");
			};
			OS_size get_dim() {return 1;};
			void initial_conditions(OS_size nb_part)
			{
				set_initial_conditions(Gaussian_Functor<data>()(nb_part));
			};
		};

template <class data>
struct Euler_Scheme_D_x
	: public Euler< D_x_t<data> >
{
	virtual OS_double CFL() {
		return 1./(get_state_variable()->size(1));
	};
};

/*! \brief Cauchy Problem for the transport equation \f$\partial_t u(t,x) = \partial_x u(t,x)\f$.
	This class initialize the Cauchy problem for the one dimensional transport equation
	\f[
		\partial_t u(t,x) = \partial_x u(t,x)
	\f];
	with \f$x \in [0,1]\f$, \f$t\ge 0\f$.

	We illustrate the behavior of the finite difference scheme
	\f[
		\delta_t u^n + \delta_{x} u^{n} = 0
	\f];
	The algorithm used is Crank Nicolson.
	The boundary conditions are given by <data> : either Dirichlet or Periodic ones.
	The initial data corresponds to a normal distribution centered in 0.5.
	For N discretization points, the CFL \f$\tau^n = \frac{1}{N}\f$ leads to the exact solution \f$u(t^n,x_i)=u(t^{n-1},x_{i+1})\f$.
*/
template <class data, class Interface = Graphical_Interface>
		class Cauchy_Euler_D_x : public CauchyProblem_traits< Euler_Scheme_D_x < data >, Interface >
		{
		public :
			virtual ~Cauchy_Euler_D_x(){}
			Cauchy_Euler_D_x(){
				initial_conditions(NUMBER_OF_PARTICLES);
			};
			virtual smart_ptr<interface_type> Create_Instance() {
				return smart_ptr<interface_type>(new Cauchy_Euler_D_x);
			};

			virtual void set_dim(OS_size dim) {
				OS_DYNAMIC_CHECK(dim == 1,"Cauchy_Euler_Wave : only dim =1");
			};
			OS_size get_dim() {return 1;};
			void initial_conditions(OS_size nb_part) {
				set_initial_conditions(Gaussian_Functor<data>()(nb_part));
			};
		};

#undef NUMBER_OF_PARTICLES

#endif